var priceTextObj = {
  "special": "福利价",
  "family": "饭团价",
  "factory": "促销价",
}

function getPriceTextBycate(cate) {
  return priceTextObj[cate] || '价格';
}

loadDataByFlow();

function loadDataByFlow() {
  layui.use(["layer", "form", "flow"], function () {
    var layer = layui.layer,
      $ = layui.jquery;
    var flow = layui.flow;

    function buildProductList(product, isVisitor) {
      var imgUrl = " ";
      var imgReg = /<img.*?(?:>|\/>)/gi;
      //匹配src属性
      var srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;
      var arr = product.content.match(imgReg);
      // console.log('所有已成功匹配图片的数组：' + arr);
      if (arr && arr[0]) {
        imgUrl = arr[0].match(srcReg)[1];
      }
      var refPriceStr2 = "";
      var shopPriceStr =
        '<p class="refPrice" style="text-decoration: line-through;">网上原价：<span>￥' +
        (product.shopPrice || product.refPrice || "未填写") +
        "</span></p>";
      if (product.category === "factory") {
        refPriceStr2 =
          '<p>建议售价：<span class="refPrice1">￥' +
          (product.refPrice || "未填写") +
          "</span></p>";
      }
      product.description = product.description || "未描述";
      var productNo = product.productNo || "";
      var productName =
        product.title.length > 28
          ? product.title.substr(0, 28) + "..."
          : product.title;
      // isVisitor
      var pdItemStr =
        '<div class="col-sm-3">' +
        '<div class="media services-wrap wow fadeInDown">' +
        '<div class="pull-left1">' +
        '<p class="productNo">' +
        productNo +
        "</p>" +
        '<a href="/products_details/' +
        product._id +
        '"><img class="img-responsive" style="height: 200px;" src="' +
        imgUrl +
        '"></a>' +
        "</div>" +
        '<div class="media-body">' +
        '<a href="/products_details/' +
        product._id +
        '" style="color: #4e4e4e;" title="' +
        product.title +
        '"><h3 class="media-heading">' +
        productName +
        "</h3></a>" +
        '<p>货物情况：<span id="acar-price">' +
        product.description +
        "</span> </p>";

      if (isVisitor) {
        pdItemStr +=
          "<p> " +
          getPriceTextBycate(productCatogary) +
          '：<span class="price"> ￥' +
          (product.price ? product.price : "未填写") +
          "</span>" +
          "</p>" +
          shopPriceStr +
          refPriceStr2;
      }
      pdItemStr += "</div></div></div>";
      return pdItemStr;
    }
    $('#product-list').empty();
    flow.load({
      elem: "#product-list", //流加载容器
      // scrollElem: "#product-list", //滚动条所在元素，一般不用填，此处只是演示需要。
      isAuto: true,
      isLazyimg: true,
      done: function (page, next) {
        //加载下一页
        // console.log(1)
        getProductData(page, 0, function (result) {
          var lis = [];
          var data = result.data;
          var isVisitor = result.isVisitor;
          for (var i = 0; i < data.length; i++) {
            var product = data[i];
            lis.push(buildProductList(product, isVisitor));
          }
          next(lis.join(""), page < result.pages); //假设总页数为 6
        });
      }
    });


  });
}

//从后台获取数据函数，获取12条产品数据
function getProductData(curr, category, cb) {
  $.post(
    "/web/get_products_by_status",
    {
      curr: curr,
      status: window.productStatus,
      keyword: window.searchKeyword || '',
      category: window.productCatogary
    },
    function (result) {
      //拼接html内容
      var tabContent = "";
      //获取后台json数据
      var mydata = result.data;
      $("#totalCount").html(result.total);
      cb(result);
    }
  );
}